package com.qinghong.gulimall.order.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.qinghong.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qinghong.gulimall.order.entity.OrderItemEntity;
import com.qinghong.gulimall.order.service.OrderItemService;
import com.qinghong.common.utils.PageUtils;
import com.qinghong.common.utils.R;



/**
 * 订单项信息
 *
 * @author qinghong
 * @email qinghong@gmail.com
 * @date 2021-10-21 13:25:15
 */
@RestController
@RequestMapping("order/orderitem")
public class OrderItemController {
    @Autowired
    private OrderItemService orderItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:orderitem:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderItemService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:orderitem:info")
    public R info(@PathVariable("id") Long id){
		OrderItemEntity orderItem = orderItemService.getById(id);

        return R.ok().put("orderItem", orderItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:orderitem:save")
    public R save(@RequestBody OrderItemEntity orderItem){
		orderItemService.save(orderItem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:orderitem:update")
    public R update(@RequestBody OrderItemEntity orderItem){
		orderItemService.updateById(orderItem);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:orderitem:delete")
    public R delete(@RequestBody Long[] ids){
		orderItemService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RabbitListener(queues = {"hello-java-queue"})
    public void recieveMessage(Message message,
                               OrderReturnReasonEntity returnReasonEntity,
                               Channel channel){
        System.out.println("接收到的信息"+returnReasonEntity);
        // channel内自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag"+deliveryTag);
        // 签收货物 非批量模式
        try {
            if (deliveryTag%2==0){
                // 收货
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物"+deliveryTag);
            }else {
                // 退货
                channel.basicNack(deliveryTag,false,true);
//                channel.basicReject();
                System.out.println("没有签收货物");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
